package cn.young.javalearning.designpattern.proxypattern.dynamicProxy.jkdproxy;

import cn.young.javalearning.designpattern.proxypattern.Shopping;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class WanNengDiWaiMaiXiaoGe implements InvocationHandler {

    private Object object;

    public Object getinstance(Object target){
        this.object = target;
        Class<?> clazz = target.getClass();
        Object o = Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
        return o;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        before();
        Object invoke = method.invoke(this.object, args);
        after();
        return invoke;
    }

    private void before() {
        System.out.println("运行前执行的代码。");
    }

    private void after() {
        System.out.println("运行后执行的代码。");
    }


}
